package com.zzh.fullscreen;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import javax.imageio.ImageIO;

public class AirPlane extends Flyer implements Award {

	public static BufferedImage[] airPlaneimage;
	private int type = 0;
	private int score = 1;
	private static List<Bullet> bulletList = null;
	private long start = 0;
	private long end = 0;
	private int delayShoot = 500;
	public static BufferedImage[] img;
	public int width;
	public int height;
	Random rand;
	final static int MAX = 4;
	static {
		try {
			airPlaneimage = new BufferedImage[MAX];
			img = new BufferedImage[MAX];
			for (int i = 0; i < MAX; i++) {
				airPlaneimage[i] = ImageIO
						.read(AirPlane.class.getClassLoader()
								.getResourceAsStream(
										"img/airplane" + (i + 1) + ".png"));
			}
			// airPlaneimage[1]=ImageIO.read(AirPlane.class.getClassLoader().getResourceAsStream("airplane2.png"));
			System.out.println("Loading->airplane2 ");
			for (int i = 0; i < MAX; i++) {
				img[i] = ImageOperate.rotateImage(AirPlane.airPlaneimage[i],
						180);
			}
			// img[1]=ImageOperate.rotateImage(AirPlane.airPlaneimage[1], 180);
			bulletList = Collections.synchronizedList(new LinkedList<Bullet>());
		} catch (IOException e) {
			System.out.println("载入敌机资源异常！");
			e.printStackTrace();
		}
	}

	public AirPlane(int x, int y, int vy) {
		super(x, y, 0, 0);
		speed = 2;
		rand = new Random(System.currentTimeMillis());

		type = rand.nextInt(2);
		if (rand.nextInt(6) == 0) {
			type = 2;
		}
		if (rand.nextInt(8) == 0) {
			type = 3;
		}

		if (type == 1) {
			speed = 3;
			delayShoot = 1000;
		}
		if (type == 3 || type == 2) {
			speed = 4;
			delayShoot = 500;
		}
		score = type + 2;
		if (vy > 0) {
			vely = speed;
		} else if (vy < 0) {
			vely = -speed;
		} else {
			vely = 0;
		}
		width = airPlaneimage[0].getWidth() / 2;
		height = airPlaneimage[0].getHeight() / 2;
		cx = x + 5;
		cy = y + 5;
		cwidth = width - 10;
		cheight = height - 10;
		start = System.currentTimeMillis();
	}

	public static List<Bullet> getBulletList() {
		return bulletList;
	}

	public void step() {
		this.updataXY();
		if (y > 50 && y < 60 || y > 400 && y < 410) {
			if (rand.nextInt(2) == 1) {
				velx = rand.nextInt(3);
			} else {
				velx = -rand.nextInt(3);
			}
		}
		end = System.currentTimeMillis();
		if (bulletList.size() < 3 && end - start >= delayShoot) {
			start = System.currentTimeMillis();
			bulletList.add(new Bullet(this.type, x + width / 2, y + height,
					DOWN));
		}
	}

	public void paint(Graphics g) {

		g.drawImage(img[type], x, y, width, height, null);
		// g.drawRect(cx,cy,cwidth,cheight);
	}

	public int getScore() {
		return score;
	}

	public int getType() {

		switch (type) {
		case 1:
			return 1;

		case 2:
			return 2;
		case 3:
			return 3;
		}
		return 0;
	}
	// public boolean intersectTo(Flyer a)
	// {
	// return
	// (this.cx<a.x+a.width&&this.cy<a.y+a.height&&this.cx+this.cwidth>a.x&&this.cy+this.cheight>a.y);
	// }
}
